<template>
  <el-dialog
      v-model="fixEditWinDialogVisible"
      title="编辑工装夹具台账"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="editFix">
        <el-form-item label="类型编码">
          <el-input v-model="editFix.fixEncode"/>
        </el-form-item>
        <el-form-item label="类型名称">
          <el-input v-model="editFix.fixName"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editFix.fixStatus">
            <el-radio :value="1" size="large">在库</el-radio>
            <el-radio :value="2" size="large">被领用</el-radio>
            <el-radio :value="3" size="large">报废</el-radio>
            <el-radio :value="4" size="large">维修中</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="保养维护类型">
          <el-select placeholder="请选择保养维护类型" style="width:150px" v-model="editFix.fixType">
            <el-option label="定期维护" :value="1"/>
            <el-option label="按使用次数维护" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="保养周期">
          <el-input v-model="editFix.fixFrequency"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editFix.fixRemark"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editFixSubmit">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import fixRequest from "@/api/fix.js";

const props = defineProps(['fixId']);
const emits = defineEmits(['cancelEdit','editComplete' ])
let fixEditWinDialogVisible = ref(false)
let editFix = ref({})


/**
 * 取消编辑的按钮
 */
function cancel() {
  emits("cancelEdit");
  fixEditWinDialogVisible.value = false;
}

/**
 * 关闭窗体前的回调函数
 */
function handleClose() {
  emits("cancelEdit");
}

function loadFixById() {
  fixRequest.selectById(props.fixId, res => {
    if (res.data) {
      editFix.value = res.data;
      fixEditWinDialogVisible.value = true;
    }
  })
}

function editFixSubmit() {
  fixRequest.update(editFix.value, res => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      fixEditWinDialogVisible.value = false;
      // 触发编辑完成的事件
      emits('editComplete');
    } else {
      ElMessage({
        type: "warning",
        message: "编辑失败"
      });
    }
  })
}

watch(() => props.fixId, () => {
  loadFixById();
});
</script>
<style scoped>

</style>